/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map; 
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.GameTimeController;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.skill.L1SkillId;
import l1j.server.server.serverpackets.S_CantMove;
import l1j.server.server.utils.SQLUtil;

// Referenced classes of package l1j.server.server.model:
// L1Teleport, L1PcInstance

public class Dungeon {

	private static Logger _log = Logger.getLogger(Dungeon.class.getName());

	private static Dungeon _instance = null;

	private static Map<String, NewDungeon> _dungeonMap = new HashMap<String, NewDungeon>();

	private enum DungeonType {
		NONE, SHIP_FOR_FI, SHIP_FOR_HEINE, SHIP_FOR_PI, SHIP_FOR_HIDDENDOCK
	};
	
	public static Dungeon getInstance() {
		if (_instance == null) {
			_instance = new Dungeon();
		}
		return _instance;
	}

	private Dungeon() {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		
		try {
			con = L1DatabaseFactory.getInstance().getConnection();

			pstm = con.prepareStatement("SELECT * FROM dungeon");
			rs = pstm.executeQuery();
			while (rs.next()) {
				int srcMapId = rs.getInt("src_mapid");
				int srcX = rs.getInt("src_x");
				int srcY = rs.getInt("src_y");
				String key = new StringBuilder().append(srcMapId).append(srcX)
				.append(srcY).toString();
				int newX = rs.getInt("new_x");
				int newY = rs.getInt("new_y");
				int newMapId = rs.getInt("new_mapid");
				DungeonType dungeonType = DungeonType.NONE;
				if (srcX == 33423 && srcY == 33502 && srcMapId == 4 //
						|| srcX == 32735 && srcY == 32794 && srcMapId == 83) { //
					dungeonType = DungeonType.SHIP_FOR_FI;
				} else if (srcX == 32936 && srcY == 33058 && srcMapId == 70 //
						|| srcX == 32732 && srcY == 32796 && srcMapId == 84) { //
					dungeonType = DungeonType.SHIP_FOR_HEINE;
				} else if (srcX == 32750 && srcY == 32874 && srcMapId == 445 //
						|| srcX == 32732 && srcY == 32796 && srcMapId == 447) { //
					dungeonType = DungeonType.SHIP_FOR_PI;
				} else if (srcX == 32297 && srcY == 33087 && srcMapId == 440 //
						|| srcX == 32735 && srcY == 32794 && srcMapId == 446) { //
					dungeonType = DungeonType.SHIP_FOR_HIDDENDOCK; 
				}
				NewDungeon newDungeon = new NewDungeon(newX, newY,
						(short) newMapId, dungeonType);
				if (_dungeonMap.containsKey(key)) {
					_log.log(Level.WARNING, "" + key);
				}
				_dungeonMap.put(key, newDungeon); 
			}
		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}

	private class NewDungeon {
		int _newX;
		int _newY;
		short _newMapId;
		DungeonType _dungeonType;
		
		private NewDungeon(int newX, int newY, short newMapId,
				DungeonType dungeonType) {
			_newX = newX;
			_newY = newY;
			_newMapId = newMapId;
			_dungeonType = dungeonType;
		
		}	
	}
	
	public boolean dg(int locX, int locY, int mapId, L1PcInstance pc) { 
		try {
			int servertime = GameTimeController.getInstance().getGameTime();
			int nowtime = servertime % 86400;
			String key = new StringBuilder().append(mapId).append(locX).append(
					locY).toString();
			if (_dungeonMap.containsKey(key)) {
				NewDungeon newDungeon = _dungeonMap.get(key);
				short newMap = newDungeon._newMapId;
				int newX = newDungeon._newX;
				int newY = newDungeon._newY;
				DungeonType dungeonType = newDungeon._dungeonType;
				boolean teleportable = false; 

				if (dungeonType == DungeonType.NONE) {
					teleportable = true;
				} else if (dungeonType == DungeonType.SHIP_FOR_FI) { //
					if ((nowtime >= 5 * 3600 && nowtime < 6 * 3600
							|| nowtime >= 9 * 3600 && nowtime < 10 * 3600
							|| nowtime >= 13 * 3600 && nowtime < 14 * 3600 || nowtime >= 17 * 3600
							&& nowtime < 18 * 3600)
							&& pc.getInventory().checkItem(40300, 1)) { //
						teleportable = true;
					}
				} else if (dungeonType == DungeonType.SHIP_FOR_HEINE) { //
					if ((nowtime >= 7 * 3600 && nowtime < 8 * 3600
							|| nowtime >= 11 * 3600 && nowtime < 12 * 3600
							|| nowtime >= 15 * 3600 && nowtime < 16 * 3600 || nowtime >= 19 * 3600
							&& nowtime < 20 * 3600)
							&& pc.getInventory().checkItem(40301, 1)) { //
						teleportable = true; 
					}
				} else if (dungeonType == DungeonType.SHIP_FOR_PI) {//
					if ((nowtime >= 7 * 3600 && nowtime < 8 * 3600
							|| nowtime >= 11 * 3600 && nowtime < 12 * 3600
							|| nowtime >= 15 * 3600 && nowtime < 16 * 3600 || nowtime >= 19 * 3600
							&& nowtime < 20 * 3600)
							&& pc.getInventory().checkItem(40302, 1)) { //
						teleportable = true;
					}
				} else if (dungeonType == DungeonType.SHIP_FOR_HIDDENDOCK) { //
					if ((nowtime >= 5 * 3600 && nowtime < 6 * 3600
							|| nowtime >= 9 * 3600 && nowtime < 10 * 3600
							|| nowtime >= 13 * 3600 && nowtime < 14 * 3600 || nowtime >= 17 * 3600
							&& nowtime < 18 * 3600)
							&& pc.getInventory().checkItem(40303, 1)) { //
						teleportable = true;
					}
				}
				if (teleportable) {
					pc.setSkillEffect(L1SkillId.ABSOLUTE_BARRIER, 2000);
					pc.stopHpRegeneration();
					pc.stopMpRegeneration();
					L1Teleport.teleport(pc, newX, newY, newMap, pc.getHeading(), false);
					return true; 
				}
			}
			return false;
		} catch (Exception exception) {
			return false;
		}
	}
}
